{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "LaTeX OCR test.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# LaTeX OCR\n",
        "In this colab you can convert an image of an equation into LaTeX code.\n",
        "## How?\n",
        "Execute the cell titled \"Setup\". The first time an error will show up. Simply execute the cell again. Everything should be fine now.\n",
        "\n",
        "Next, execute the cell below and upload the image(s).\n",
        "\n",
        "> Note: You can probably also run this project locally and with a GUI. Follow the steps on [GitHub](https://github.com/lukas-blecher/LaTeX-OCR)"
      ],
      "metadata": {
        "id": "aaAqi3wku23I"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "DQM_PKeCuzWR"
      },
      "outputs": [],
      "source": [
        "#@title Setup\n",
        "%reload_ext autoreload\n",
        "%autoreload\n",
        "import PIL\n",
        "!pip install Pillow -U -qq\n",
        "if int(PIL.__version__[0]) < 9:\n",
        "    print('Mandatory restart: Execute this cell again!')\n",
        "    import os\n",
        "    os.kill(os.getpid(), 9)\n",
        "!pip install pix2tex -qq\n",
        "!pip install opencv-python-headless==4.1.2.30 -U -qq\n",
        "\n",
        "def upload_files():\n",
        "  from google.colab import files\n",
        "  from io import BytesIO\n",
        "  uploaded = files.upload()\n",
        "  return [(name, BytesIO(b)) for name, b in uploaded.items()]\n",
        "\n",
        "from pix2tex import cli as pix2tex\n",
        "from PIL import Image\n",
        "model = pix2tex.LatexOCR()\n",
        "\n",
        "from IPython.display import HTML, Math\n",
        "display(HTML(\"<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/\"\n",
        "             \"latest.js?config=default'></script>\"))\n",
        "table = r'\\begin{array} {l|l} %s  \\end{array}'"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "imgs = upload_files()\n",
        "predictions = []\n",
        "for name, f in imgs:\n",
        "    img = Image.open(f)\n",
        "    math = model(img)\n",
        "    print(math)\n",
        "    predictions.append('\\\\mathrm{%s} & \\\\displaystyle{%s}'%(name, math))\n",
        "Math(table%'\\\\\\\\'.join(predictions))"
      ],
      "metadata": {
        "id": "CjrR3O07u3uH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "ZqCH-4XoCkMO"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}